package com.rongc.client.freight.business.mine.view.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.SystemUtil;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.view.widget.SpaceItemDecoration;
import com.rongc.client.freight.business.carrier.request.api.CarListApi;
import com.rongc.client.freight.business.carrier.view.adapter.RecyclerChoiceConveyanceAdapter;
import com.rongc.client.freight.business.mine.model.CarBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Bruce on 2017/7/19.
 */
public class CarHostActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.recycler)
    RecyclerView mRecycler;
    RecyclerChoiceConveyanceAdapter mAdapter;
    List<CarBean> carBeanList;

    @OnClick({R.id.lyt_create})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.lyt_create:
                ActivityUtils.startActivity(this, CarCreateActivity.class);
                break;
        }
    }
    Response.Listener<CarBean[]> respCarListListener = new Response.Listener<CarBean[]>() {

        @Override
        public void onResponse(CarBean[] response) {
            if (HandleCode.requestSuccess()) {
                if(response != null && response.length != 0){
                    carBeanList.clear();
                }
                for (int i = 0; i < response.length; i++) {
                    carBeanList.add(response[i]);
                }
                mAdapter.notifyDataSetChanged();
            } else {
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
        }
    };


    @Override
    public void initView() {
        initToolbar(R.string.car_host_title);
        carBeanList = new ArrayList<>();
        mAdapter = new RecyclerChoiceConveyanceAdapter(this,carBeanList);
        GridLayoutManager mGridLayoutManager = new GridLayoutManager(this,1);
        mRecycler.addItemDecoration(new SpaceItemDecoration(SystemUtil.dip2px(this,1f)));
        mRecycler.setLayoutManager(mGridLayoutManager);
        mRecycler.setAdapter(mAdapter);
        RequestManager.getInstance().call(new CarListApi(
                new CarListApi.CarListParams(UniApplication.getInstance().getUserInfo().getUserId()),
                respCarListListener, errorListener));
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_car_host;
    }
}
